package cn.elead.it.sso.sample.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import cn.elead.it.sso.sample.interceptor.CrossInterceptor;
import cn.elead.it.sso.sample.interceptor.OptionsInterceptor;

/**
 * <p>
 * Spring Boot MVC配置
 * </p>
 *
 * @author luopeng
 */
@Configuration
public class WebMvcAutoConfiguration implements WebMvcConfigurer {


	@Override
	public void addCorsMappings(CorsRegistry registry) {
		// https://segmentfault.com/a/1190000018554528?utm_source=tag-newest
		// 添加映射路径,允许跨域访问的路径
		registry.addMapping("/**")
				// 放行哪些原始域,允许跨域访问的源
				.allowedOrigins("*")
				// 是否发送Cookie信息
				.allowCredentials(true)
				// 放行哪些原始域,允许请求方法(请求方式)
				.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
				// 放行哪些原始域,允许头部设置(头部信息)
				.allowedHeaders("Content-Type"," x-token", "Authorization", "sso_sessionid")
				// 暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
				.exposedHeaders("Header1", "Header2").maxAge(3600);
	}
	

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new CrossInterceptor()).addPathPatterns("/**");
		registry.addInterceptor(new OptionsInterceptor()).addPathPatterns("/**");
	}
}
